package com.lanou3g.musicenjoy.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lanou3g.musicenjoy.entity.New;
import com.lanou3g.musicenjoy.R;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 15/10/21.
 */
public class NewAdapter extends BaseAdapter{
    private List<New.ResultEntity.MvListEntity> datas;
    private Context mContext;

    public NewAdapter (Context c){
        mContext =c;
    }
    public void initData(List<New.ResultEntity.MvListEntity> datas){
        if (this.datas==null){
            this.datas=new ArrayList<New.ResultEntity.MvListEntity>();
        }else {
            this.datas.clear();
        }
        this.datas=datas;
        notifyDataSetChanged();

    }

    @Override
    public int getCount() {
        return datas!=null&&datas.size()>0?datas.size():0;
    }

    @Override
    public Object getItem(int position) {
        return datas!=null&&datas.size()>0?datas.get(position):null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder=null;
        if (convertView==null){
            convertView= LayoutInflater.from(mContext).inflate(R.layout.fragment_new_item,null);
            holder=new ViewHolder();
            holder.imageView= (ImageView) convertView.findViewById(R.id.newImageView);
            holder.textView= (TextView) convertView.findViewById(R.id.newTextView);
            holder.view= (TextView) convertView.findViewById(R.id.newView);
            convertView.setTag(holder);
        }else {
           holder= (ViewHolder) convertView.getTag();
        }
        New.ResultEntity.MvListEntity entity= (New.ResultEntity.MvListEntity) getItem(position);
        if (entity!=null){
            Picasso.with(mContext).load(entity.getThumbnail2()).into(holder.imageView);
            holder.textView.setText(entity.getTitle());
            holder.view.setText(entity.getArtist());
        }
        return convertView;
    }
    private class ViewHolder{
        private ImageView imageView;
        private TextView textView;
        private TextView view;

    }
}
